package com.yummy.service.impl;

import com.yummy.dto.AddressBookCopy;
import com.yummy.entity.AddressBook;
import com.yummy.entity.Dish;
import com.yummy.mapper.AddressBookMapper;
import com.yummy.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yinbo
 */
@Service
public class AddressServiceImpl implements AddressService {
	@Autowired
	AddressBookMapper addressBookMapper;
	@Override
	public List<AddressBook> queryByUser(Integer userId) {
		List<AddressBook> addressBookList = addressBookMapper.selectByUser(userId);

		return addressBookList;
	}

	@Override
	public AddressBook queryDefault(Integer id) {

		return addressBookMapper.selectDefaultByUser(id);
	}

	@Override
	public boolean editAddressBook(AddressBook addressBook) {
		return addressBookMapper.updateAddress(addressBook)>0;
	}

	@Override
	public boolean addAddressBook(AddressBook addressBook) {
		return addressBookMapper.insertAddress(addressBook)>0;
	}

	@Override
	public boolean removeAddressBooks(String... ids) {
		return addressBookMapper.delAddress(ids)>0;
	}


	@Override
	public boolean updateIsDefault(Integer id, Integer isDefault) {
		return addressBookMapper.updateDefault(id,isDefault)>0;
	}

	@Override
	public AddressBook findAddressById(Integer id) {
		AddressBook addressBook = addressBookMapper.selectAddressById(id);
		return addressBook;
	}

	@Override
	public List<AddressBookCopy> queryByUsers(Integer userId) {
		List<AddressBook> addressBookList = addressBookMapper.selectByUser(userId);
		//这是后端转化，就是构造个数据类，从新赋值就可以了

		ArrayList<AddressBookCopy> addressBookCopies = new ArrayList<>();
		AddressBookCopy addressBookCopy = null;
		for (AddressBook addressBook : addressBookList) {
			System.out.println("address:"+addressBook);
			addressBookCopy = new AddressBookCopy();
			addressBookCopy.setId(String.valueOf(addressBook.getId()));
			addressBookCopy.setName(String.valueOf(addressBook.getConsignee()));
			addressBookCopy.setAddress(String.valueOf(addressBook.getProvinceName() + addressBook.getCityName() + addressBook.getDistrictName() + addressBook.getDetail()));
			addressBookCopy.setTel(String.valueOf(addressBook.getPhone()));
			if (addressBook.getIsDefault() == 1) {
				addressBookCopy.setisDefault(true);
			} else {
				addressBookCopy.setisDefault(false);
			}
			addressBookCopies.add(addressBookCopy);
		}
		return addressBookCopies;
	}


}
